import { _decorator, Component, MotionStreak, Vec2, Node, math, Vec3, Color, Sprite } from 'cc';
import { DrawingLineManager } from './DrawingLineManager';
import { EventManager, MyEvent } from './EventManager';
const { ccclass, property } = _decorator;

@ccclass('DrawingLineItem')
export class DrawingLineItem extends Component {
    @property(Sprite)
    Sprite: Sprite = null;

    @property(MotionStreak)
    Trail: MotionStreak = null;

    @property
    circleRadius: number = 5;         // 圆半径

    @property
    itemRadius: number = 0.5;          // item的半径

    @property(Vec2)
    velocity: Vec2 = new Vec2(0, -1);  // 初始速度

    @property
    speed: number = 100;              // 初始速度系数

    @property
    addSpeed: number = 1;             // 每次碰撞增加的速度

    @property
    minSpeed: number = 5;

    @property
    maxSpeed: number = 5;

    private _color: Color = new Color();
    private _isPause: boolean = false;

    protected start(): void {
        this._color = this.getRandomBrightColor();
        this.Trail.color = this._color;
        this.Sprite.color = this._color;
    }

    update(dt: number) {
        if (!DrawingLineManager.Instance.IsStart) return;
        if (this._isPause) return;

        const delta = new Vec2(this.velocity.x, this.velocity.y).multiplyScalar(dt * this.speed);

        let cowPos2D = new Vec2(this.node.getWorldPosition().x, this.node.getWorldPosition().y).add(delta);

        // === 检查边界 ===
        const center2D = new Vec2(DrawingLineManager.Instance.CententPos.x, DrawingLineManager.Instance.CententPos.y);
        const dist = cowPos2D.clone().subtract(center2D).length();

        if (dist + this.itemRadius >= this.circleRadius) {
            // 计算法线
            const normal = cowPos2D.clone().subtract(center2D).normalize();
            const inverse = normal.clone();
            // 反射速度
            const reflectDir = this.reflect(this.velocity.clone(), normal.clone());

            // 更新速度和加速
            this.velocity = reflectDir;
            this.speed = math.clamp(this.speed + this.addSpeed, this.minSpeed, this.maxSpeed);
            // 推回边界
            cowPos2D = center2D.add(normal.multiplyScalar(this.circleRadius - this.itemRadius));
            DrawingLineManager.Instance.addLine(this.node, this._color, inverse);
        }

        // === 应用位置 ===
        this.node.setWorldPosition(new Vec3(cowPos2D.x, cowPos2D.y, this.node.getWorldPosition().z));
    }

    /** 向量反射 */
    private reflect(v: Vec2, n: Vec2): Vec2 {
        // v - 2*(v·n)*n
        const dot = v.dot(n);
        return v.subtract(n.multiplyScalar(2 * dot));
    }

    //获取比较亮的颜色
    getRandomBrightColor(): Color {
        let r = Math.floor(128 + Math.random() * 128);
        let g = Math.floor(128 + Math.random() * 128);
        let b = Math.floor(128 + Math.random() * 128);
        return new Color(r, g, b, 255);
    }

    private pause() {
        this._isPause = !this._isPause;
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.PAUSE, this.pause, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.PAUSE, this.pause, this);
    }

}


